<template>
  <div :id="chart_id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
export default {
  name: 'BarModelScore',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '705px'
    },
    barData: {
      type: Array,
      default: null
    }
  },
  data() {
    return {
      chart: null,
      chart_id: 'month'
    }
  },
  watch: {
    barData: {
      handler(newValue, oldValue) {
        this.initChart()
      },
      deep: true
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    if (typeof this.barData !== 'undefined' && this.barData != null) {
      this.initChart()
    }
  },
  methods: {
    initChart() {
      const legendData = ['高风险', '中风险', '低风险']
      const mark = []
      const heightRisk = []
      const middleRisk = []
      const lowRisk = []
      const yAxisData = []
      // console.log(this.barData)
      this.barData.forEach((item) => {
        const label = [item.varUnit === null || item.varUnit === 'null' ? item.varLabel : item.varLabel + '(' + item.varUnit + ')', item.comment !== 'undefined' && item.comment !== null ? item.comment : item.varVal, item.varScore]
        yAxisData.push(label)
        if (item.label === '高风险') {
          mark.push(1)
          heightRisk.push(item.woe)
          middleRisk.push(0)
          lowRisk.push(0)
        } else if (item.label === '低风险') {
          mark.push(2)
          heightRisk.push(0)
          middleRisk.push(0)
          lowRisk.push(item.woe)
        } else {
          mark.push(3)
          heightRisk.push(0)
          middleRisk.push(item.woe)
          lowRisk.push(0)
        }
      })
      // console.log(lowRisk)
      // console.log(yAxisData)
      this.chart = echarts.init(document.getElementById(this.chart_id))
      this.chart.setOption({
        tooltip: {
          formatter: function(p) {
            var r = p.name.split(',')
            // console.log(p)
            var result
            if (r[0].indexOf('比') !== -1 || r[0].indexOf('率') !== -1) {
              result = '变量：' + r[0] + '<br>' + '<div>' + '取值：' + (r[1] * 100).toFixed(2) + '%' + '<br>' + '得分：' + r[2] + '</div>' + '<span style="display:inline-block;margin-right:5px;margin-bottom:0;border-radius:10px;width:9px;height:9px;background-color: ' + p.color + ';"></span>'
            } else {
              result = '变量：' + r[0] + '<br>' + '<div>' + '取值：' + r[1] + '<br>' + '得分：' + r[2] + '</div>' + '<span style="display:inline-block;margin-right:5px;margin-bottom:0;border-radius:10px;width:9px;height:9px;background-color: ' + p.color + ';"></span>'
            }
            result += p.seriesName + '<br>'
            return result
          }
        },
        legend: {
          data: legendData,
          x: '80%',
          y: '30px'
        },
        grid: {
          left: 190,
          top: 44,
          bottom: 9,
          right: 195
        },
        xAxis: {
          type: 'value',

          splitLine: {
            show: false
          },
          axisLabel: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          }

        },
        yAxis: {
          type: 'category',
          data: yAxisData,
          show: false,
          inverse: true,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          }
        },
        series: [{
          name: '高风险',
          type: 'bar',
          barGap: '-100%',
          barWidth: '20px',
          data: heightRisk,
          itemStyle: {
            normal: {
              color: '#f84343'
            }
          },
          label: {
            show: true,
            position: 'right',
            color: '#333333',
            fontSize: 14,
            formatter: function(param) {
              if (param.value > 0) return param.value
              else if (mark[param.dataIndex] === 1) return param.value.toFixed(2)
              else return ''
            },
            offset: [10, 0]
          }
        },
        {
          name: '中风险',
          type: 'bar',
          barGap: '-100%',
          barWidth: '20px',
          data: middleRisk,
          itemStyle: {
            normal: {
              color: '#ff9900'
            }
          },
          label: {
            show: true,
            position: 'right',
            color: '#333333',
            fontSize: 14,
            formatter: function(param) {
              if (param.value > 0) return param.value.toFixed(2)
              // else if (param.value === 0) return param.value
              else if (mark[param.dataIndex] === 3) return param.value.toFixed(2)
              else return ''
            },
            offset: [10, 0]
          }
        },
        {
          name: '低风险',
          type: 'bar',
          barGap: '-100%',
          barWidth: '20px',
          data: lowRisk,
          itemStyle: {
            normal: {
              color: '#00b9ff'
            }
          },
          label: {
            show: true,
            position: 'right',
            color: '#333333',
            fontSize: 14,
            formatter: function(param) {
              if (param.value > 0) return param.value.toFixed(2)
              else if (mark[param.dataIndex] === 2) return param.value.toFixed(2)
              else return ''
            },
            offset: [10, 0]
          }
        }
        ]
      })
    }
  }
}
</script>

<style scoped>

</style>
